package com.lucenly.card.utils;

import android.app.Activity;

import com.lucenly.card.bean.ShareBean;
import com.lucenly.card.constants.UrlDefend;
import com.siberiadante.toastutils.ToastUtil;
import com.vise.xsnow.dialog.PromptDialog;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.mode.DownProgress;
import com.vise.xsnow.utls.SharedPreferencesUtil;

/**
 * ================================================
 * 作    者：LuCenly(落尘)
 * 版    本：1.0
 * 创建日期：2018/7/29 16:46
 * 描    述：TODO
 * ================================================
 */
public class PublicUtil {
    public static void getRule(int type,ACallback callback){
        ViseHttp.GET(UrlDefend.GET_RULE)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .addParam("typeId",type+"")
                .request(callback);
    }
    public static void getBanner(int type,ACallback callback){
        ViseHttp.GET(UrlDefend.GET_BANNER)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .addParam("typeId",type+"")
                .request(callback);
    }
    public static void getConfig(int type,ACallback callback){
        ViseHttp.GET(UrlDefend.GET_CONFIG)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .addParam("id",type+"")
                .request(callback);
    }
    public static void getKefu(ACallback callback){
        ViseHttp.GET(UrlDefend.KEFU)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(callback);
    }

    public static void getShareConfig(final Activity context, final shareInterface callback){
        final PromptDialog promptDialog = new PromptDialog(context);
        promptDialog.showLoading("正在获取分享信息...");
        ViseHttp.GET(UrlDefend.SHARE_CONFIG)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<ShareBean>() {
                    @Override
                    public void onSuccess(ShareBean data) {
//                        getBimtMap(context,promptDialog,data,callback);
                        promptDialog.dismiss();
                        callback.shareSuscess(data);
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        promptDialog.dismiss();
//                        Toast.makeText(context, errMsg, Toast.LENGTH_SHORT).show();
                        ToastUtil.showSingletonShort(errMsg);}
                });
    }

    public static void getBimtMap(final Activity context, final PromptDialog promptDialog, final ShareBean shareBean, final shareInterface callback){
//        String substring = shareBean.shareLogo.substring(shareBean.shareLogo.lastIndexOf("."), shareBean.shareLogo.length());
        ViseHttp.DOWNLOAD(shareBean.shareLogo)
                .setFileName("share_logo.png")
                .request(new ACallback<DownProgress>() {
                    @Override
                    public void onSuccess(DownProgress downProgress) {
                        promptDialog.dismiss();
                        callback.shareSuscess(shareBean);
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        promptDialog.dismiss();
//                        Toast.makeText(context, errMsg, Toast.LENGTH_SHORT).show();
                        ToastUtil.showSingletonShort(errMsg);}
                });
    }

    public interface shareInterface{
        void shareSuscess(ShareBean shareBean);
    }
}
